/**
 * Created by LXhan on 2017/12/1.
 */
var zTreeNodes;
var buildingId;
var type;
var setting = {
    view: {
        showLine: false,
        showIcon: false,
        addDiyDom: addDiyDom
    },
    data: {
        simpleData: {
            enable: true
        }
    }
};
/**
 * 自定义DOM节点
 */
function addDiyDom(treeId, treeNode) {
    var spaceWidth = 15;
    var liObj = $("#" + treeNode.tId);
    var aObj = $("#" + treeNode.tId + "_a");
    var switchObj = $("#" + treeNode.tId + "_switch");
    var icoObj = $("#" + treeNode.tId + "_ico");
    var spanObj = $("#" + treeNode.tId + "_span");
    aObj.attr('title', '');
    aObj.append('<div class="diy swich"></div>');
    var div = $(liObj).find('div').eq(0);
    switchObj.remove();
    spanObj.remove();
    icoObj.remove();
    div.append(switchObj);
    div.append(spanObj);
    var spaceStr = "<span style='height:1px;display: inline-block;width:" + (spaceWidth * treeNode.level) + "px'></span>";
    switchObj.before(spaceStr);
    var editStr = '';
    editStr += '<div class="diy">' + (treeNode.BUILDING_NAME == null ? '&nbsp;' : treeNode.BUILDING_NAME) + '</div>';
    var corpCat = '<div title="' + treeNode.RATES + '">' + treeNode.RATES + '</div>';
    editStr += '<div class="diy">' + (treeNode.RATES == '-' ? '&nbsp;' : corpCat ) + '</div>';
    editStr += '<div class="diy">' + (treeNode.CARD == null ? '&nbsp;' : treeNode.CARD ) + '</div>';
    editStr += '<div class="diy">' + formatHandle(treeNode) + '</div>';
    aObj.append(editStr);
}
/**
 * 查询数据
 */
function query() {
    //初始化列表
//        zTreeNodes = data;
    //初始化树
    $.fn.zTree.init($("#dataTree"), setting, getTreeNodes(type,buildingId));
    //添加表头
    var li_head = ' <li class="head"><a><div class="diy">编号</div><div class="diy">楼宇</div><div class="diy">倍率</div>' +
        '<div class="diy">插卡式</div><div class="diy">操作</div></a></li>';
    var rows = $("#dataTree").find('li');
    if (rows.length > 0) {
        rows.eq(0).before(li_head)
    } else {
        $("#dataTree").append(li_head);
        $("#dataTree").append('<li ><div style="text-align: center;line-height: 30px;" >无符合条件数据</div></li>')
    }
}
/**
 * 根据权限展示功能按钮
 * @param treeNode
 * @returns {string}
 */
function formatHandle(treeNode) {
    var htmlStr = '';
    var y;
    if(type == "b938139a48ed4344a11a7180b48eede6"){
        y = 0;
    }else {
        y = 1;
    }
    htmlStr += '<div class="icon_div icon_edit"><a class="btn_edit" title="修改" href="javascript:add(\'' + y + '\',0,1,\'' + treeNode.id + '\')">修改</a></div>';
    htmlStr += '<div class="icon_div icon_change"><a class="btn_change" title="更换" href="javascript:changeMeter(\'' + y + '\',\'' + treeNode.id + '\')">换表</a></div>';
    htmlStr += '<div class="icon_div icon_del"><a class="btn_del" title="删除" href="javascript:del(\'' + treeNode.id + '\')">删除</a></div>';
    return htmlStr;
}

$(function () {
    //初始化数据
    type = $("#type").val();
    buildingId = $("#buildingId").val();
    query();
    $("#save").click(function () {
        updatePrice($("#price").val(),$("#priceId").val());
    })
})
function getTreeNodes(type, buildingId) {
    var nodes;
    $.ajax({
        type: "post",
        url: "/hydropower/findTreeTable",
        async: false,
        data: {
            buildingId: buildingId,
            type: type
        },
        success: function (rs) {
            nodes = rs.data;
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
        }
    });
    return nodes;
}

function updatePrice(price,priceId) {
    $.ajax({
        type: "post",
        url: "/hydropowerprice/save",
        async: false,
        data: {
            id: priceId,
            buildingId: buildingId,
            price: price,
            type:type
        },
        success: function (rs) {
            if (rs.errcode == "01") {
                layer.msg(rs.errmsg);
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
        }
    });
}

/**
 * 打开新增或修改页面
 * @param t 0-电表；1-水表
 * @param s 0-新增水（电）表；1-新增子表
 * @param y 0-新增；1-修改
 */
function add(t, s, y,id) {
    var title;
    var pid = '';
    var pCode = '';
    if (y == 0) {
        if (s == 0) {
            if (t == 0) {
                title = "新增电表";
            } else {
                title = "新增水表";
            }
        } else {
            title = "新增子表";
            if (t == 0) {
                pid = $("#eletable").find("tbody").find("tr").eq(0).attr("id");
                pCode = $("#eletable").find("tbody").find("tr").eq(0).find("td").eq(0).text();
            } else {
                pid = $("#watertable").find("tbody").find("tr").eq(0).attr("id");
                pCode = $("#watertable").find("tbody").find("tr").eq(0).find("td").eq(0).text();
            }
        }
    } else {
        var msg = "";
        if (t == 0) {
            title = "修改电表";
            msg = "请先选择要操作的电表";
        } else {
            title = "修改水表";
            msg = "请先选择要操作的水表";
        }
        if(id == null || id == ""){
            layer.msg(msg);
            return false;
        }
    }
    layer.open({
        type: 2,
        title: title,
        shadeClose: false,
        shade: [0.3],
        maxmin: true, //开启最大化最小化按钮
        area: ['900px', '90%'],
        scrollbar: false, //屏蔽滚动条
        content: 'water_electricity_edit.html?t=' + t + '&s=' + s + '&y=' + y + '&id=' + id + '&pid=' + pid + '&pCode=' + pCode + '&buildingId=' + activebuildingID
    });
}

function del(id) {
    var tips;
    var msg = "";
    if (type == 'b938139a48ed4344a11a7180b48eede6') {//电表
        tips = "确定要删除该电表吗";
        msg = "请先选择要操作的电表";
    } else {
        tips = "确定要删除该水表吗";
        msg = "请先选择要操作的水表";
    }
    if(id == null || id == ""){
        layer.msg(msg);
        return false;
    }
    layer.msg(tips, {
        time: 0
        , btn: ['确定', '取消']
        , shade: 0.3
        , yes: function (index) {
            layer.close(index);
            $.ajax({
                type: "post",
                url: "/hydropower/del",
                async: false,
                data: {
                    id: id
                },
                success: function (rs) {
                    if (rs.errcode == "00") {
                        if (type == 'b938139a48ed4344a11a7180b48eede6') {
                            initEleTable();
                        }else {
                            initWatTable();
                        }
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                }
            });
        }
    });

}

function changeMeter(t,id) {
    var title = "";
    var msg = "";
    if (t == 0) {
        title = "更换电表";
        msg = "请先选择要操作的电表";
    } else {
        title = "更换水表";
        msg = "请先选择要操作的水表";
    }
    if(id == null || id == ""){
        layer.msg(msg);
        return false;
    }
    layer.open({
        type: 2,
        title: title,
        shadeClose: false,
        shade: [0.3],
        maxmin: true, //开启最大化最小化按钮
        area: ['900px', '90%'],
        scrollbar: false, //屏蔽滚动条
        content: 'water_electricity_change.html?t=' + t + '&id=' + id
    });
}